package cn.uniqueinfo.dp.util;

import cn.uniqueinfo.dp.vo.FormButtonVO;
import cn.uniqueinfo.dp.vo.FormGridColumnVO;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:朱元勋
 * @mail:395910063@qq.com
 * @date:2019-01-28 09:48:14
 * @description:
 */
public class BizUtil {

    /** 拼接新增sql */
    public static String insertSql(Map<String, Object> params) {
        String formName = (String) params.get("formName");
        params.remove("formName");

        String tableName = (String) params.get("tableName");
        params.remove("tableName");
        params.remove("id");

        StringBuffer fieldSB = new StringBuffer();
        StringBuffer valueSB = new StringBuffer();

        for (String key : params.keySet()) {
            fieldSB.append(" " + key + " ,");
            valueSB.append(" '" + params.get(key) + "' ,");
        }

        String fields = fieldSB.toString();
        fields = fields.substring(0, fields.length() - 1);
        String values = valueSB.toString();
        values = values.substring(0, values.length() - 1);

        String sql = "insert into " + tableName + " (" + fields + ") values (" + values + ")";
        System.out.println(sql);
        return sql;
    }
    /** 拼接更新sql */
    public static String updateSql(Map<String, Object> params) {
        String formName = (String) params.get("formName");
        params.remove("formName");

        String tableName = (String) params.get("tableName");
        params.remove("tableName");
        Integer id = Integer.parseInt((String) params.get("id"));
        params.remove("id");

        StringBuffer sb = new StringBuffer();

        for (String key : params.keySet()) {
            sb.append(key + "='" + params.get(key) + "',");
        }

        String sql = sb.toString();
        sql = sql.substring(0, sql.length() - 1);

        sql = "update " + tableName + " set " + sql + " where id=" + id;
        System.out.println(sql);
        return sql;
    }

    /** 拼接按钮 */
    public static String genButtonInfos(List<FormButtonVO> buttonList) {
        if (buttonList == null || buttonList.size() == 0) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        for (FormButtonVO button : buttonList) {
            sb.append(
                    "<a id=\""
                            + button.getBtnName()
                            + "\" href=\"#\" class=\"easyui-linkbutton\" data-options=\"iconCls:'"
                            + button.getBtnIcon()
                            + "',plain:true\" >"
                            + button.getBtnTitle()
                            + "</a>");
        }
        return sb.toString();
    }

    /** 拼接表格头部 */
    public static String genTableHead(List<FormGridColumnVO> columnList) {
        if (columnList == null || columnList.size() == 0) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        sb.append(
                "<th data-options=\"field:'id',width:80,align:'center',checkbox:true\">序号</th>\n");
        for (FormGridColumnVO column : columnList) {
            if ("id".equalsIgnoreCase(column.getColumnLabel())) {
                continue;
            }
            sb.append(
                    "<th data-options=\"field:'"
                            + column.getColumnLabel()
                            + "',align:'"
                            + column.getAlign()
                            + "',halign:'"
                            + column.getHalign()
                            + "',width:"
                            + column.getWidth()
                            + "\">"
                            + column.getText()
                            + "</th>\n");
        }

        return sb.toString();
    }

    public static void main(String[] args) {
        Map<String, Object> params = new HashMap<>();
        params.put("formName", "biz_demo_001");
        params.put("tableName", "biz_demo");
        params.put("id", 0);
        params.put("code", "code");
        params.put("commnet", "备注");
        String isql = updateSql(params);
        System.out.println(isql);
    }
}
